<?php

namespace app\admin\traits;

use RuntimeException;

trait SettingContent {

	protected array $typeList = [
		'text', 'number', 'password', 'mobile', 'email', 'id_card', 'url', 'ip',
		'textarea', 'switch', 'select', 'multi_select', 'image', 'multi_image',
		'date', 'date_range', 'datetime', 'datetime_range', 'year', 'year_range',
		'year_month', 'year_month_range', 'map', 'color', 'icon', 'editor',
	];

	/**
	 * 获取配置内容
	 * @param $param
	 * @return array
	 */
	protected function getContent($param): array {
		$content = [];
		foreach ($param['config_name'] as $key => $value) {
			if (($param['config_name'][$key]) === ''
				|| ($param['config_field'][$key] === '')
				|| ($param['config_type'][$key] === '')
			) {
				throw new RuntimeException('设置信息不完整');
			}

			if ($param['config_option'][$key] === '' && in_array($param['config_type'][$key], ['select', 'multi_select'])) {
				throw new RuntimeException('设置信息不完整');
			}

			$this->checkName($value);
			$this->checkField($param['config_field'][$key]);
			$this->checkType($param['config_type'][$key]);

			$content[] = [
				'name' => $value,
				'field' => $param['config_field'][$key],
				'type' => $param['config_type'][$key],
				'content' => strip_tags($param['config_content'][$key]),
				'option' => strip_tags($param['config_option'][$key]),
			];
		}
		return $content;
	}
	protected function getDetail($param): array {

		$content = [];
		foreach ($param['config_name'] as $key => $value) {
			if (($param['config_name'][$key]) === ''
				|| ($param['config_num'][$key] === '')
				|| ($param['config_price'][$key] === '')
				|| ($param['config_spec'][$key] === '')
				|| ($param['config_brand'][$key] === '')
				|| ($param['config_compute_id'][$key] === '')
			) {
				throw new RuntimeException('设置信息不完整');
			}

			$content[] = [
				'name' => $value,
				'spec' => $param['config_spec'][$key],
				'brand' => $param['config_brand'][$key],
				'num' => $param['config_num'][$key],
				'price' => $param['config_price'][$key],
				'compute_id' => $param['config_compute_id'][$key],
			];
		}
		return $content;
	}
	protected function getMenuDetail($param): array {

		$content = [];
		foreach ($param['config_type'] as $key => $value) {
			if (($param['config_type'][$key]) === ''
				|| ($param['config_num'][$key] === '')
				|| ($param['config_spec'][$key] === '')
				|| ($param['config_brand'][$key] === '')
			) {
				throw new RuntimeException('设置信息不完整');
			}

			$content[] = [
				'type_id' => $value,
				'num' => $param['config_num'][$key],
				'spec_id' => $param['config_spec'][$key],
				'brand_id' => $param['config_brand'][$key],
			];
		}
		return $content;
	}
	protected function getMealDetail($param): array {

		$content = [];
		foreach ($param['config_menu'] as $key => $value) {
			if (($param['config_menu'][$key]) === ''
			) {
				throw new RuntimeException('设置信息不完整');
			}

			$content[] = [
				'menu_id' => $value,
			];
		}
		return $content;
	}
	protected function getDoseDetail($param): array {

		$content = [];
		foreach ($param['config_meal'] as $key => $value) {
			if (($param['config_meal'][$key]) === ''
				|| ($param['config_time'][$key]) === ''
				|| ($param['config_num'][$key]) === ''
			) {
				throw new RuntimeException('设置信息不完整');
			}

			$content[] = [
				'meal_id' => $value,
				'time' => $param['config_time'][$key],
				'num' => $param['config_num'][$key],
			];
		}
		return $content;
	}
	protected function getDoseDetail2($param): array {

		$content = [];
		foreach ($param['config_type'] as $key => $value) {
			if (($param['config_type'][$key]) === ''
				|| ($param['config_spec'][$key]) === ''
				|| ($param['config_brand'][$key]) === ''
				|| ($param['config_num'][$key]) === ''
			) {
				throw new RuntimeException('设置信息不完整');
			}

			$content[] = [
				'type_id' => $value,
				'spec_id' => $param['config_spec'][$key],
				'brand_id' => $param['config_brand'][$key],
				'num' => $param['config_num'][$key],
			];
		}
		return $content;
	}
	protected function getCheckDetail($param): array {

		$content = [];
		foreach ($param['config_type'] as $key => $value) {
			if (($param['config_type'][$key]) === ''
				|| ($param['config_spec'][$key]) === ''
				|| ($param['config_brand'][$key]) === ''
				|| ($param['config_num'][$key]) === ''
				|| ($param['config_shou_num'][$key]) === ''
				|| ($param['config_real_num'][$key]) === ''
			) {
				throw new RuntimeException('设置信息不完整');
			}

			$content[] = [
				'type_id' => $value,
				'spec_id' => $param['config_spec'][$key],
				'brand_id' => $param['config_brand'][$key],
				'num' => $param['config_num'][$key],
				'real_num' => $param['config_real_num'][$key],
				'shou_num' => $param['config_shou_num'][$key],
			];
		}
		return $content;
	}
	protected function getOfferDetail($param): array {

		$content = [];
		foreach ($param['config_type'] as $key => $value) {
			if (($param['config_type'][$key]) === ''
				|| ($param['config_spec'][$key]) === ''
				|| ($param['config_brand'][$key]) === ''
				|| ($param['config_num'][$key]) === ''
				|| ($param['config_price'][$key]) === ''
				|| ($param['config_money'][$key]) === ''
			) {
				throw new RuntimeException('设置信息不完整');
			}

			$content[] = [
				'type_id' => $value,
				'spec_id' => $param['config_spec'][$key],
				'brand_id' => $param['config_brand'][$key],
				'num' => $param['config_num'][$key],
				'price' => $param['config_price'][$key],
				'money' => round($param['config_num'][$key] * $param['config_price'][$key],2),
			];
		}
		return $content;
	}
	protected function getSendDetail($param): array {

		$content = [];
		foreach ($param['config_type'] as $key => $value) {
			if (($param['config_type'][$key]) === ''
				|| ($param['config_spec'][$key]) === ''
				|| ($param['config_brand'][$key]) === ''
				|| ($param['config_num'][$key]) === ''
				|| ($param['config_price'][$key]) === ''
				|| ($param['config_shou_num'][$key]) === ''
				|| ($param['config_wei_num'][$key]) === ''
				|| ($param['config_avg_price'][$key]) === ''
				|| ($param['config_money'][$key]) === ''
				|| ($param['config_status'][$key]) === ''
			) {
				throw new RuntimeException('设置信息不完整');
			}

			$content[] = [
				'type_id' => $value,
				'spec_id' => $param['config_spec'][$key],
				'brand_id' => $param['config_brand'][$key],
				'num' => $param['config_num'][$key],
				'price' => $param['config_price'][$key],
				'shou_num' => $param['config_shou_num'][$key],
				'wei_num' => $param['config_wei_num'][$key],
				'avg_price' => $param['config_avg_price'][$key],
				'money' => round($param['config_num'][$key] * $param['config_price'][$key],2),
				'beizu' => $param['config_beizhu'][$key],
				'status' => $param['config_status'][$key],
			];
		}
		return $content;
	}
	protected function getDetail2($param): array {

		$content = [];
		foreach ($param['config_name'] as $key => $value) {
			if (($param['config_name'][$key]) === ''
				|| ($param['config_num'][$key] === '')
				|| ($param['config_price'][$key] === '')
				|| ($param['config_money'][$key] === '')
				|| ($param['config_spec'][$key] === '')
				|| ($param['config_brand'][$key] === '')
			) {
				throw new RuntimeException('设置信息不完整');
			}

			$content[] = [
				'name' => $value,
				'spec' => $param['config_spec'][$key],
				'brand' => $param['config_brand'][$key],
				'num' => $param['config_num'][$key],
				'price' => $param['config_price'][$key],
				'money' => $param['config_money'][$key],
			];
		}
		return $content;
	}
	protected function checkName($name): void {
		$pattern = '/^[\x{4e00}-\x{9fa5}\x{9fa6}-\x{9fef}\x{3400}-\x{4db5}\x{20000}-\x{2ebe0}a-zA-Z0-9_\-]+$/u';
		if (!preg_match($pattern, $name)) {
			throw new RuntimeException('名称的值只能是汉字、字母、数字和下划线_及破折号-');
		}
	}

	protected function checkField($name): void {
		$pattern = '/^[a-zA-z]\w{2,49}$/';
		if (!preg_match($pattern, $name)) {
			throw new RuntimeException('字段的值只能是字母、数字、下划线组成，字母开头，3-50位');
		}
	}

	protected function checkType($name): void {
		if (!in_array($name, $this->typeList, true)) {
			throw new RuntimeException('类型非法');
		}
	}

}